﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LaoLuo
{
    /// <summary>
    /// 提供Type类型的扩展方法
    /// </summary>
    public static class TypeExtensions
    {
        /// <summary>
        /// 获取类型是否为数值类型
        /// </summary>
        /// <param name="type">要检查是否为数值类型的类型</param>
        /// <returns>类型是否为数值类型</returns>
        public static bool IsNumeric(this Type type)
        {
            switch (Type.GetTypeCode(type))
            {
                case TypeCode.Byte:
                case TypeCode.Decimal:
                case TypeCode.Double:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.SByte:
                case TypeCode.Single:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                    return true;
                default:
                    return false;
            }
        }

        /// <summary>
        /// 获取指定类型的默认值
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object GetDefault(this Type type)
        {
            return type.IsValueType ? Activator.CreateInstance(type) : null;
        }
    }
}
